package com.mars.algorithm;

/**
 * @Author King
 * @create 2020/6/30 13:36
 */
public class KingListQueue<T> {

    private class Node<T> {
        T value;
        Node<T> next;
    }

    private int size;
    private Node<T> head;
    private Node<T> tail;

    public KingListQueue() {
        this.size = 0;
    }

    public boolean enqueue(T t) {
        Node<T> newNode = new Node<T>();
        newNode.value = t;
        if (size == 0) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            tail = tail.next;
        }
        size++;
        return true;
    }

    public T dequeue() {
        if (size == 0) {
            return null;
        }
        if (size == 1) {
            tail = null;
        }
        T t = null;
        t = head.value;
        head = head.next;
        size--;
        return t;
    }


    public static void main(String[] args) {
        KingListQueue<String> queue = new KingListQueue<String>();
        queue.enqueue("1");
        queue.enqueue("2");
        queue.enqueue("3");
        queue.enqueue("4");
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
    }


}
